package com.market.controller;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.market.dto.GoodsDto;
import com.market.dto.GoodsTypeDto;
import com.market.entity.Goods;
import com.market.service.GoodsService;
import com.market.service.GoodsTypeService;
import com.market.vo.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
 * @ClassName GoodsController
 * @Description TODO
 * @Author 1
 * @Date 2022/3/28 17:02
 * @Version 1.0
 */
@RestController
@RequestMapping("goods")
public class GoodsController {
    @Autowired
    private GoodsService goodsService;
    @Autowired
    private GoodsTypeService goodsTypeService;
    //新增商品
    @PostMapping("addGods")
    public R<List<Goods>> addGods(@RequestBody Goods goods) {
        if (goodsService.addGoods(goods)) {
            return new R<>(200,"商品新增成功");
        }else{
            return new R<>(201,"商品新增失败");
        }
    }

    //上架商品
    @GetMapping("shelveGoods")
    public R<List<Goods>> shelveGoods(@RequestParam("goodsId") int goodsId) {
        if (goodsService.shelveGoods(goodsId)) {
            return new R<>(200,"商品上架成功");
        }
        else{
            return new R<>(201,"商品上架失败");
        }
    }

    //下架商品
    @GetMapping("offShelveGoods")
    public R<List<Goods>> offShelveGoods(@RequestParam("goodsId") int goodsId) {
        if (goodsService.offShelveGoods(goodsId)) {
            return new R<>(200,"商品下架成功");
        }else{
            return new R<>(201,"商品下架失败");
        }
    }

    //删除商品
    @GetMapping("deleteGoods")
    public R<List<Goods>> deleteGoods(int goodsId) {
        if (goodsService.deleteGoods(goodsId)) {
            return new R<>(200,"商品删除成功");
        }else{
            return new R<>(201,"商品删除失败");
        }
    }

    //修改商品属性（名称，售价，描述，每日可销售数量）
    @GetMapping("updateGoodsProperty")
    public R<List<Goods>> updateGoodsProperty(int goodsId,String goodsName,double price,String description,int typeId,int sellNum){
        if(goodsService.updateGoodsProperty(goodsId,goodsName,price,description,typeId,sellNum)){
            return new R<>(200,"修改商品属性成功");

        }else{
            return new R<>(201,"修改商品属性失败");
        }
    }

    //根据商品类别id查询商品
    @GetMapping("queryGoodsByTypeId")
    public R<List<Goods>> queryGoodsByTypeId(int typeId, int currentPage, int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> result=goodsService.queryGoodsByTypeId(typeId);
        if(result.size()!=0){
            return new R(200,"根据类别名查找商品成功",new PageInfo<>(result));
        }else{
            return new R<>(201,"根据类别名查找商品失败");
        }
    }

    //根据商品id查询某个商品
    @GetMapping("queryGoodsByGoodsId")
    public R queryGoodsByGoodsId(int goodsId){
        Goods result=goodsService.queryGoodsByGoodsId(goodsId);
        if(result!=null){
           return new R(200,"查询商品成功",result);
        }else{
            return new R(201,"查询商品失败");
        }
    }

    @RequestMapping("queryAllGoods")
    public R queryAllGoods(){
        List<GoodsDto> goods = goodsService.queryGoodsByState();
        return new R(200,"success",goods);
    }

    //查全部商品
    @GetMapping("queryTotalGoods")
    public R queryTotalGoods( int currentPage, int pageSize){
        PageHelper.startPage(currentPage,pageSize);
        List<Goods> goods = goodsService.queryAllGoods();
        if(goods.size()!=0){
            return new R(200,"查询全部商品成功",new PageInfo<>(goods));
        }else{
            return new R(201,"查询全部商品失败");
        }
    }

    @RequestMapping("queryByType")
    public R queryGoodsByType(int typeId){
        List<GoodsDto> result=goodsService.queryByType(typeId);
        return new R(200,"查询该分类下的商品成功",result);
    }
}
